package com.teamelite.exmgmt;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class cAddNewUser extends Activity {
	public Button btnSaveData;
	public DBHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_caddnewuser);
		db = new DBHelper(this);
		// Set Listener Untuk Tombol
        btnSaveData = (Button)findViewById(R.id.btnSaveData);
        btnSaveData.setOnClickListener(btnSaveDataListener);
	}
	
	View.OnClickListener btnSaveDataListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Spinner cbGender = (Spinner)findViewById(R.id.cbGender);
			EditText nama,umur,tinggi,berat;
			String gender;
			nama = (EditText)findViewById(R.id.txtNama);
			umur = (EditText)findViewById(R.id.txtUmur);
			tinggi = (EditText)findViewById(R.id.txtTinggi);
			berat = (EditText)findViewById(R.id.txtBerat);
			Object selectedGender = cbGender.getSelectedItem();
			// Isi Gender
			if(selectedGender.equals("Laki-Laki")){
				gender = "L";
			}else{
				gender = "P";
			}
			
			Toast t = Toast.makeText(cAddNewUser.this, "", Toast.LENGTH_SHORT);
			t.setGravity(Gravity.CENTER|Gravity.TOP, 0, 20);
			// Cek Apakah Input Masih Kosong?
			if(!nama.getText().toString().equals("") && 
			   !umur.getText().toString().equals("") && 
			   !tinggi.getText().toString().equals("") &&
			   !berat.getText().toString().equals("")){
					ContentValues values=new ContentValues();
					values.put("nama", ""+nama.getText());
					values.put("jk", ""+gender);
					values.put("umur", ""+umur.getText());
					values.put("tinggi", ""+tinggi.getText());
					values.put("berat", ""+berat.getText());
					int oldCount = db.getCount();
					db.addPengguna(values);
					if(db.getCount()>oldCount){
						t.setText("Data Berhasil Disimpan");
						t.show();
						Intent intents = new Intent(cAddNewUser.this, cListUser.class);
			        	cAddNewUser.this.startActivity(intents);
			        	finish();
					}else{
						t.setText("Data Gagal Disimpan");
						t.show();
					}
			}else{
				t.setText("Form Masih Kosong. Harap Mengisi Dengan Benar");
				t.show();
			}
		}
	};

}
